package org.hagen.mmia.yars.engine.corpora;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

import org.hagen.mmia.yars.collection.Collection;
import org.hagen.mmia.yars.collection.Document;
import org.hagen.mmia.yars.collection.Topic;

/*
 * very simple parser
 */
public class OhsuMed extends Reader {

	static final int INVALID = -1;
	static final int TITLE	 = 1;
	static final int AUTHOR	 = 2;
	static final int ID		 = 3;
	static final int UID 	 = 4;
	static final int CONTENT = 5;

	static final int MAX_DOCUMENTS = 90000000;
	
	static int documentIdx = 0;
	
	private static boolean verbose = false;		

	public void readAbstract( ) {
		readAbstract( "C:\\DATA\\OhsuMed\\ohsumed.87" );
		readAbstract( "C:\\DATA\\OhsuMed\\ohsumed.88" );
		readAbstract( "C:\\DATA\\OhsuMed\\ohsumed.89" );
		readAbstract( "C:\\DATA\\OhsuMed\\ohsumed.90" );
		readAbstract( "C:\\DATA\\OhsuMed\\ohsumed.91" );
	}

	private void readAbstract( String filename ) {
		File file = new File(filename);

		Abstract document = null;

		try {
			Scanner in = new Scanner(file);
			int mode = OhsuMed.INVALID;

			while (in.hasNextLine()) {

				String line = in.nextLine();

				if (line.matches("^\\.I\\s[0-9]+")) {

					if (documentIdx > OhsuMed.MAX_DOCUMENTS-1 )
						break;

					if (document != null) {
						document.normalize( STOPPING, STEMMING );
						Reader.abstracts.add(document);
					}

					document 			 = new Abstract();
					document.tmp_content = new ArrayList<String>();
					document.tmp_title   = new ArrayList<String>();
					document.index = documentIdx++;
					
					mode = OhsuMed.ID;
				} else if (line.matches("^\\.U")) {
					mode = OhsuMed.UID;
					continue;
				} else if (line.matches("^\\.S")) {
					mode = OhsuMed.INVALID;
					continue;
				} else if (line.matches("^\\.M")) {
					mode = OhsuMed.INVALID;
					continue;
				} else if (line.matches("^\\.T")) {
					mode = OhsuMed.TITLE;
					continue;
				} else if (line.matches("^\\.P")) {
					mode = OhsuMed.INVALID;
					continue;
				} else if (line.matches("^\\.W")) {
					mode = OhsuMed.CONTENT;
					continue;
				} else if (line.matches("^\\.A")) {
					mode = OhsuMed.AUTHOR;
					continue;
				}

				if (document != null) {
					switch (mode) {
					case OhsuMed.ID:
						document.tmp_ID = line;
						
						if( verbose ) {
							System.out.println( "readAbstract: read tmp_ID: " + document.tmp_ID );
						}
						
						break;

					case OhsuMed.UID:
						break;
					case OhsuMed.TITLE:
						line = line.toLowerCase();
						document.tmp_title.add( line );
						
						if( verbose ) {
							System.out.println( "readAbstract: read tmp_title: " + line );
						}
						
						break;
					case OhsuMed.AUTHOR:
						break;
					case OhsuMed.CONTENT:
						line = line.toLowerCase();
						document.tmp_content.add( line );
						
						if( verbose ) {
							System.out.println( "readAbstract: read tmp_content: " + line );
						}							
						
					default:
						break;
					}
				}
			}

			if (document != null) {
				document.normalize( STOPPING, STEMMING );
				Reader.abstracts.add(document);
			}

			in.close();

		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}

	}

	public void readTopic( ) {
		String filename = new String( "C:\\DATA\\OhsuMed\\queries2" );
		File file = new File(filename);
		
		try {
			Scanner in = new Scanner(file);
			int mode = OhsuMed.INVALID;
			int index   = 0;

			Topic topic = null; 

			while (in.hasNextLine()) {

				String line = in.nextLine();
				if (line.matches("^\\.I[\\s]+[0-9]+")) {
					mode = OhsuMed.ID;
				} else if (line.matches("^\\.B")) {
					mode = OhsuMed.INVALID;
					continue;
				} else if (line.matches("^\\.W")) {
					mode = OhsuMed.CONTENT;
					continue;
				}

				switch (mode) {
				case OhsuMed.ID:

					String stringId = (line.split("  ")[1]);

					if( verbose ) {
						System.out.println( "Topic ID read: " + stringId + " (" + index + ")" );
					}
					
					topic = new Topic( index, stringId );
					Collection.addTopic(topic);

					index++;
					break;
					
				case OhsuMed.CONTENT:
					line = line.replaceAll("[\\,.\\s/\\-()]+", " ").toLowerCase();
					
					// stopping
					if( Reader.STOPPING ) {
						line = doStopping( line );
					}
					// stemming
					if( Reader.STEMMING ) {
						line = doStemming( line );
					}
					
					topic.appendNeed( line.trim() );

					if( verbose ) {
						System.out.println( "Topic content read: " + line );
					}
					break;

				case OhsuMed.INVALID:
					// skip line
					break;

				default:
					break;
				}
				
			}
			in.close();

		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}			
	}
	
	public void readRelevance( ) {
		readPRelevance( "C:\\DATA\\OhsuMed\\pdrel.i" );
	}
	
	// assumed that topic list exists
	public void readPRelevance( String filename ) {
		File file = new File(filename);
		
		try {
			Scanner in = new Scanner(file);

			int prevID = -1;
			Topic topic = null;

			while (in.hasNextLine()) {

				int ID = in.nextInt();

				if ( ID != prevID ) {
					topic = Collection.getTopicByID( new String( ID + "" ) );
					if( topic == null ) {
						System.err.println( "readRelevance: topic by ID not found: " + ID );
						break;
					}
				}
				prevID = ID;

				int docID = in.nextInt();
				Document document = Collection.getDocument( new String( docID + "" ) );
				if( document == null ) {
					System.err.println( "readPRelevance: document not found: " + ID );
					break;
				}

				if( document.getContent() == null || document.getContent().trim().isEmpty() ) {
					//System.err.println( "readRelevance: document is empty " + docID );
					continue;
				}
				
				// possibly relevant: weight = 3
				if( verbose ) {
					System.out.println( "Possibly relevant document read: " + docID + " for topic " + ID + " " + topic.relSet.size() );
				}
				
				// add a document to the topic set (set based representation)
				topic.relSet.add( new String( docID + "" ) );
			}

			in.close();
		} catch (FileNotFoundException e1 ) {
			e1.printStackTrace();
		
		}
	}
}


